package com.zbkj.service.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zbkj.common.model.user.UserBill;
import com.zbkj.common.vo.user.UserDatavalVo;
import com.zbkj.service.dao.user.UserBillMapper;
import com.zbkj.service.service.user.UserBillService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户账单表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-12
 */
@Service
public class UserBillServiceImpl extends ServiceImpl<UserBillMapper, UserBill> implements UserBillService {

    @Override
    public List<UserBill> userDataValVoToPojo(UserDatavalVo userDatavalVo) {
        List<UserBill> userBillList = Lists.newArrayList();
        UserBill userBill = new UserBill();
        userBill.setUserId(userDatavalVo.getUserId());
        userBill.setPmtype(userDatavalVo.getPmtype());
        userBill.setCategory(userDatavalVo.getCategoryEnum().getCategory());
        userBill.setCardDataval(userDatavalVo.getCardDataval());
        userBill.setDataval(userDatavalVo.getDataval());
        userBill.setRepDataval(userDatavalVo.getRepDataval());
        userBill.setSourceType(userDatavalVo.getSourceType());
        userBill.setSourceId(userDatavalVo.getSourceId());
        userBill.setSourceNo(userDatavalVo.getSourceNo());
        userBill.setYearPid(userDatavalVo.getYearPid());
        userBill.setMark(userDatavalVo.getCategoryEnum().getRemark());
        userBillList.add(userBill);
        return userBillList;
    }

    @Override
    public UserBill getUserBill(long userId, int category, int sourceType, long sourceId, String sourceNo) {
        LambdaQueryWrapper<UserBill> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserBill::getDeleteFlag, 0)
                .eq(UserBill::getUserId,userId)
                .eq(UserBill::getCategory, category)
                .eq(UserBill::getSourceType, sourceType)
                .eq(UserBill::getSourceId, sourceId);
        if(StringUtils.isNotBlank(sourceNo)) {
            wrapper.eq(UserBill::getSourceNo, sourceNo);
        }
        wrapper.orderByDesc(UserBill::getId);
        wrapper.last(" limit 1");
        return this.getOne(wrapper, false);
    }
}
